package cn.bugstack.domain.activity.service.trial.node;

import cn.bugstack.domain.activity.model.entity.MarketProductEntity;
import cn.bugstack.domain.activity.model.entity.TrialBalanceEntity;
import cn.bugstack.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import cn.bugstack.domain.activity.service.trial.factory.DefaultActivityStrategyFactory.DynamicContext;
import cn.bugstack.types.design.framework.tree.StrategyHandler;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import javax.annotation.Resource;

import cn.bugstack.types.enums.ResponseCode;
import cn.bugstack.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 开关节点
 * @author hsj
 * @date 2025/2/12 10:58
 */
@Slf4j
@Service
public class SwitchNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DynamicContext, TrialBalanceEntity> {

	@Resource
	private MarketNode marketNode;


	@Override
	public TrialBalanceEntity doApply(MarketProductEntity requestParameter, DynamicContext dynamicContext)
		throws ExecutionException, InterruptedException, TimeoutException {

		String userId = requestParameter.getUserId();
		if (activityRepository.downgradeSwitch()){
			throw new AppException(ResponseCode.UN_ERROR);
		}

		if (!activityRepository.cutRange(userId)){
			throw new AppException(ResponseCode.UN_ERROR);
		}

		return router(requestParameter,dynamicContext);
	}

	@Override
	public StrategyHandler<MarketProductEntity, DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter,
		DynamicContext dynamicContext) {
		return marketNode;
	}

}
